package com.zh.work.service.platform.channel.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zh.work.dao.platform.channel.QuickRouteMapper;
import com.zh.work.entity.channel.QrCodeRouteMerchEntity;
import com.zh.work.entity.channel.QuickRouteEntity;
import com.zh.work.service.platform.channel.QuickRouteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 快捷路由设置业务逻辑实现层
 * @Author: Li.HuiNeng
 * @Create in: 2019/09/06
 * @Version: 1.0.1
 */
@Service
public class QuickRouteServiceImpl extends ServiceImpl<QuickRouteMapper, QuickRouteEntity> implements QuickRouteService {

    private  static final Logger log = LoggerFactory.getLogger(QuickRouteServiceImpl.class);

    @Autowired
    private QuickRouteMapper mapper;


    @Override
    public IPage<QuickRouteEntity> selectQuickRoutePages(Page<QuickRouteEntity> page, QuickRouteEntity quickRouteEntity) {
        return mapper.getPagesQuickRoutes(page,
                quickRouteEntity.getBranchNo(),
                quickRouteEntity.getSettleType(), quickRouteEntity.getStatus(),
                quickRouteEntity.getRouteName(), quickRouteEntity.getMerchNo());
    }

    @Transactional
    @Override
    public int putAddQuickRoute(QuickRouteEntity quickRouteEntity) throws Exception {
        // 新增快捷路由规则
        if (mapper.insert(quickRouteEntity) <= 0) return -1;
        // 解析快捷路由商户列表
        List<QrCodeRouteMerchEntity> list = JSON.parseArray(quickRouteEntity.getMerchantRouteJson(), QrCodeRouteMerchEntity.class);
        if (list != null) {
            if (list.size() > 0) {
                list.forEach(f -> f.setRouteId(quickRouteEntity.getId()));
                // 新增快捷路由商户
                if (mapper.addQuickRouteMerchant(list) <= 0) return -2;
            } else
                log.info("快捷路由商户为空!");
        }
        return 1;
    }

    @Transactional
    @Override
    public int putModQuickRouteById(QuickRouteEntity quickRouteEntity) throws Exception {
        // 查询已存在快捷路由商户数量
        Map<String, Long> map = mapper.getQuickMerchantNum(quickRouteEntity.getId());
        if (map.get("num") > 0) {
            if (mapper.delQuickMerchantById(quickRouteEntity.getId()) <= 0) return -1;
        }
        // 解析快捷路由商户列表
        List<QrCodeRouteMerchEntity> list = JSON.parseArray(quickRouteEntity.getMerchantRouteJson(), QrCodeRouteMerchEntity.class);
        if (list != null) {
            if (list.size() > 0) {
                list.forEach(f -> f.setRouteId(quickRouteEntity.getId()));
                // 新增快捷路由商户
                if (mapper.addQuickRouteMerchant(list) <= 0) return -2;
            } else log.info("快捷路由商户为空!");
        }
        // 修改快捷路由信息
        if (mapper.updateById(quickRouteEntity) <= 0) return -3;
        return 1;
    }
}
